package com.airsystem.pos.bengkel.model.dialog;

import java.awt.GridBagLayout;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.airsystem.pos.bengkel.component.DialogFrame;
import com.airsystem.pos.bengkel.entity.DetailKendaraan;
import com.airsystem.pos.bengkel.entity.DetailLayanan;
import com.airsystem.pos.bengkel.entity.Kendaraan;
import com.airsystem.pos.bengkel.entity.Layanan;
import com.airsystem.pos.bengkel.util.TextUtil;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterServiceDialogModel extends DialogFrame {
	private static final String[] COLUMNS = { "Jenis Service", "Merk Kendaraan", "Sub Merk Kendaraan",
											  "Harga", "ID" };

	protected int row = 0;

	protected GridBagLayout baseLayout = new GridBagLayout();
	protected GridBagLayout tableLayout = new GridBagLayout();

	protected JPanel basePanel = new JPanel();
	protected JPanel tablePanel = new JPanel();

	protected JScrollPane tableScroll = new JScrollPane();

	protected JTable tableData = new JTable();

	protected JLabel searchLabel = new JLabel();

	protected JComboBox searchCombo = new JComboBox();

	protected JTextField searchField = new JTextField();

	protected JButton searchButton = new JButton();

	protected DefaultTableModel tableModel = new DefaultTableModel(new Object[][] {}, COLUMNS) {
		boolean[] canEdit = new boolean[] { false, false, false,
											false, false };

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return canEdit[columnIndex];
		}
	};

	private DetailLayanan detailLayanan;

	public DetailLayanan getDetailLayanan() {
		return detailLayanan;
	}

	public void setDetailLayanan(DetailLayanan detailLayanan) {
		this.detailLayanan = detailLayanan;
	}

	protected DetailLayanan getComponentData() {
		DetailLayanan detailLayanan = new DetailLayanan();
		detailLayanan.setLayanan(new Layanan());
		detailLayanan.getLayanan().setNama(searchField.getText());

		return detailLayanan;
	}

	protected DetailLayanan getSearchData() {
		DetailLayanan detailLayanan = new DetailLayanan();
		detailLayanan.setChoice(searchCombo.getSelectedIndex());

		String input = searchField.getText().trim();

		switch (searchCombo.getSelectedIndex()) {
		case 1: {
			detailLayanan.setLayanan(new Layanan());
			detailLayanan.getLayanan().setNama(input);
			break;
		}

		case 2: {
			if (!TextUtil.isNumberOnly(input)) {
				throw new IllegalArgumentException("Wrong chosen search combo");
			}

			detailLayanan.setHarga(new BigDecimal(input));
			break;
		}

		case 3: {
			detailLayanan.setDetail_kendaraan(new DetailKendaraan());
			detailLayanan.getDetail_kendaraan().setKendaraan(new Kendaraan());
			detailLayanan.getDetail_kendaraan().getKendaraan().setMerk(input);
			break;
		}

		case 4: {
			detailLayanan.setDetail_kendaraan(new DetailKendaraan());
			detailLayanan.getDetail_kendaraan().setSub_merk(input);
			break;
		}

		default:
			throw new IllegalArgumentException("Wrong chosen search combo");
		}

		return detailLayanan;
	}
}